<script type="text/javascript">  
sco=true;
function foo() {
	sco = undefined;
	return this.sco;
}
alert(foo());
alert(new foo());
</script>  

==================================================================================

<!Doctype HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
	function global(){
		var xyz='globalstr';		
	}
	var xyz=12;
	function abc(){
		var xyz='str';
		xyz=true;
	}
	abc();
	alert(xyz);
	xyz='override';
	global();
	alert(xyz);
//-->
</SCRIPT>

===================================================================

<script>
function firstMethod(){
	for(var i=0; i<10; i++){
		z = i;
		document.writeln("Z : "+z+"<br/>");
	}
}
firstMethod();
document.writeln("Z value after first loop : "+z+"<br/>"); // Z value after first loop : 9
document.writeln("accessing i value : "+i+"<br/>"); // script error

</script>

output
-------
Z : 0
Z : 1
Z : 2
Z : 3
Z : 4
Z : 5
Z : 6
Z : 7
Z : 8
Z : 9
Z value after first loop : 9

After this, it gives the javascript Error saying i is not defined

Note::
 the global variable 'z' will printed only after calling firstMethod().

---------------example 2------------------------

<script>

	var sum = 10;

	function add_t()  {
	   var sum = sum + 20; /*varible sum referers to local variable*/
	   document.writeln("sum inside menthod : "+sum+"</br>");

	   sum = this.sum + 20; /*varible this.sum referers to global variable*/
	   document.writeln("sum inside menthod afer using global variable called 'sum': "+sum+"</br>");
	};
	add_t();
	sum = sum + 1;
	document.writeln("sum outside menthod : "+sum+"</br>");

</script>

-----------------------------

<script type="text/javascript" language="JavaScript">
<!--
var globalVar1 = '(G1) global variable declared with var keyword';
globalVar2 = '(G2) global variable without var keyword';
var someBoolean = true;
if (someBoolean) {
	var globalVar3 = '(G3) global variable declared with var keyword inside if statement';
}

function sampleFunction()
{
	var localVar1 = '(L1) local variable declared with var keyword';
	localVar2 = '(L2) local variable declared without var keyword';
	if (someBoolean) {
		var localVar3 = '(L3) local variable declared with var keyword inside if statement';
	}
	
	document.writeln('<b>You can reference all local variables inside the same function the variables are declared.</b><br />');
	document.writeln(localVar1 + '<br />');
	document.writeln(localVar2 + '<br />');
	document.writeln(localVar3 + '<br /><br />');
	
	document.writeln('<b>You can reference all global variables inside any function.</b><br />');
	document.writeln(globalVar1 + '<br />');
	document.writeln(globalVar2 + '<br />');
	document.writeln(globalVar3 + '<br /><br />');
}

sampleFunction();

document.writeln('<b>Outside a function, you can only reference local variables that are not declared with the var keyword.</b><br />');
document.writeln('Commented out. Cannot reference (L1)<br />'); //document.writeln(localVar1 + '<br />');
document.writeln(localVar2 + '<br />');
document.writeln('Commented out. Cannot reference (L3)<br /><br />'); //document.writeln(localVar3 + '<br />');

document.writeln('<b>Outside a function, you can reference all global variables.</b><br />');
document.writeln(globalVar1 + '<br />');
document.writeln(globalVar2 + '<br />');
document.writeln(globalVar3);
//-->
</script>


------------------------
 

 <script>
	// a globally-scoped variable 
	var a=1; 
	 
	// global scope 
	function one(){ 
		alert(a);  
	} 
	 
	// local scope 
	function two(a){ 
		alert(a); 
	} 
	 
	// local scope again 
	function three(){ 
	  var a = 3; 
	  alert(a); 
	} 
	 
	// Intermediate: no such thing as block scope in javascript 
	function four(){ 
		if(true){ 
			var a=4; 
		} 
	 
		alert(a); // alerts '4', not the global value of '1' 
	} 
	 
	 
	// Intermediate: object properties 
	function Five(){ 
		this.a = 5; 
	} 
	 
	 
	// Advanced: closure 
	var six = function(){ 
		var foo = 6; 
	 
		return function(){ 
			// javascript "closure" means I have access to foo in here,  
			// because it is defined in the function in which I was defined. 
			alert(foo); 
		} 
	}() 
	 
	 
	// Advanced: prototype-based scope resolution 
	function Seven(){ 
	  this.a = 7; 
	} 
	 
	// [object].prototype.property loses to [object].property in the scope chain 
	Seven.prototype.a = -1; // won't get reached, because 'a' is set in the constructor above. 
	Seven.prototype.b = 8; // Will get reached, even though 'b' is NOT set in the constructor. 
	 
	 
	 
	// These will print 1-8 
	one(); 
	two(2); 
	three(); 
	four(); 
	alert(new Five().a); 
	six(); 
	alert(new Seven().a); 
	alert(new Seven().b); 

 </script>